# encoding: UTF-8

import  os
from xml.etree import ElementTree as ET

def parse(xmlDir):
    packages = []

    list = os.listdir(xmlDir)
    for file in list:
        if file.endswith('xml'):
            package     = {}

            document    = ET.parse(xmlDir + '/' + file)
            root        = document.getroot()

            package['package'] = root.attrib['package']
            package['comment'] = root.attrib['comment']
            package['baseURL'] = root.attrib['baseURL']

            protocls = []

            #解析协议
            protoclElements = root.findall('protocl')
            for ele in protoclElements:
                protocl = {}
                protocl['method']   = ele.attrib['method']
                protocl['return']   = ele.attrib['return']
                protocl['comment']  = ele.attrib['comment']

                #解析协议参数
                params = []
                for paramEle in ele.findall('param'):
                    param = {}
                    param['name']       = paramEle.attrib['name']
                    param['type']       = paramEle.attrib['type']
                    param['demoVal']    = paramEle.attrib['demoVal']
                    param['comment']    = paramEle.attrib['comment']
                    params.append(param)

                protocl['params'] = params
                protocls.append(protocl)

            #解析Bean
            beanElements = root.findall('bean')
            beans = []
            for ele in beanElements:
                bean = {}
                bean['name']    = ele.attrib['name']
                bean['comment'] = ele.attrib['comment']
                if ele.attrib.has_key('extends'):
                    bean['extends'] = ele.attrib['extends']
                else:
                    bean['extends'] = ''

                #解析Bean属性
                params = []
                for paramEle in ele.findall('param'):
                    param = {}
                    param['name']       = paramEle.attrib['name']
                    param['type']       = paramEle.attrib['type']
                    param['comment']    = paramEle.attrib['comment']
                    params.append(param)
                bean['params'] = params
                beans.append(bean)

            package['beans']    = beans
            package['protocls'] = protocls
            packages.append(package)
    return  packages